package br.com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public abstract class Dao<T extends Object> {

    private Connection conexao;
    private PreparedStatement stmt;
    private ResultSet rs;

    public Connection getConnection() {
        try {
            return conexao = Conexao.getConnection("jdbc:postgresql://" + Conexao.host + ":" + Conexao.port + "/" + Conexao.database, Conexao.user, Conexao.password);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public void closeConnection() {
        try {
            conexao.close();
        } catch (SQLException e) {
            System.err.println("Erro ao fechar conexão. " + e.getMessage());
        }
    }

    public ResultSet executeQuery(String sql) {
        try {
            stmt = getConnection().prepareStatement(sql);
            rs = stmt.executeQuery();
            return rs;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public abstract void salvar( T obj ) throws SQLException;

    public abstract void atualizar( T obj ) throws SQLException;

    public abstract void deletar( T obj ) throws SQLException;

    public abstract  ArrayList<T>  selecionar() throws SQLException;
   
}
